package cn.gson.springmvc.commons;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class DateConverter implements Converter<String, Date> {
            @Override
            public Date convert(String s) {
                Date date = null;
        if(s==null || "".equals(s)){
            //统一发到错误页面
        }else{
            if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$",s)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = sdf.parse(s);
                } catch (ParseException e) {
                    e.printStackTrace();
                    //发送错误页面
                }
            }
        }
        return date;
    }
}
